<!--
 * @Description: 菜单权限管理页面
 * @Date: 2023-04-05 10:04:51
 * @LastEditTime: 2023-04-10 22:58:11
 * @Author: Black
 * @FilePath: \store_admin\src\views\category\list.vue
-->
<template>
  <el-card shadow="never" class="border-0">

    <ListHeader @create="handleCreate" @refresh="getData" />

    <el-tree :data="tableData" :props="{ label: 'name', children: 'child' }" v-loading="loading" node-key="id">
      <template #default="{ node, data }">
        <div class="custom-tree-node">
          <el-icon :size="18" color="#7537ae">
            <PriceTag />
          </el-icon>
          <span class="ml-3">{{ data.name }}</span>
          <div class="ml-auto">
            <el-button class="mr-3" text type="primary" size="small" @click="openGoodsDrawer(data)"
              :loading="data.goodsDrawerLoading">推荐商品</el-button>
            <el-switch class="mr-3" :modelValue="data.status" :active-value="1" :inactive-value="0"
              @change="handleStatusChange($event, data)" />
            <el-button text type="primary" size="small" @click.stop="handleEdit(data)">修改</el-button>
            <el-popconfirm title="是否要删除该记录？" confirm ButtonText="确认" cancelButtonText="取消" @confirm="handleDelete(data.id)"
              width="200px">
              <template #reference>
                <el-button text type="primary" size="small">删除</el-button>
              </template>
            </el-popconfirm>
          </div>
        </div>
      </template>

    </el-tree>

    <!-- 新建分类 -->
    <FormDrawer ref="formDrawerRef" :title="drawerTitle" @submit="handleSubmit">
      <el-form :model="form" ref="formRef" :rules="rules" label-width="80px" :inline="false">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="名称"></el-input>
        </el-form-item>
      </el-form>
    </FormDrawer>

    <GoodsDrawer ref="GoodsDrawerRef"></GoodsDrawer>
  </el-card>
</template>

<script setup>
import { ref } from "vue"
import ListHeader from "~/components/ListHeader.vue"
import FormDrawer from "~/components/FormDrawer.vue"
import GoodsDrawer from "./components/GoodsDrawer.vue"
import { useInitTable, useInitForm } from "~/composables/useCommon.js"
import {
  getCategoryList,
  createCategory,
  updateCategory,
  updateCategoryStatus,
  deleteCategory
} from "~/api/category.js"

const {
  loading,
  tableData,
  getData,
  handleDelete,
  handleStatusChange
} = useInitTable({
  getList: getCategoryList,
  onGetListSuccess: (res) => {
    tableData.value = res.map(o => {
      o.goodsDrawerLoading = false
      return o
    })
  },
  delete: deleteCategory,
  updateStatus: updateCategoryStatus
})

const {
  formDrawerRef,
  formRef,
  form,
  rules,
  drawerTitle,
  handleSubmit,
  handleCreate,
  handleEdit
} = useInitForm({
  form: {
    name: "",
  },
  rules: {
    name: [{
      required: true,
      message: '权限名称不能为空',
      trigger: 'blur'
    }],
    condition: [{
      required: true,
      message: '后端规则不能为空',
      trigger: 'blur'
    }],
  },
  getData,
  update: updateCategory,
  create: createCategory
})

const GoodsDrawerRef = ref(null)
const openGoodsDrawer = (data) => GoodsDrawerRef.value.open(data)

</script>

<style scoped>
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  font-size: 15px;
  padding-right: 8px;
}

:deep(.el-tree-node__content) {
  padding: 20px 0;
  height: 50px;
}</style>